package com.lei.controller;

import com.alibaba.fastjson.JSON;
import com.lei.entity.LayDate;
import com.lei.entity.Type;
import com.lei.service.TypeService;
import com.lei.service.impl.TypeServiceImpl;
import com.lei.util.BaseDb;
import com.lei.util.PageBean;
import com.lei.util.TransBean;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Lei Chu
 * @Date 2020/10/21 17:15
 */
@WebServlet("/typeServlet/*")
public class TypeServlet extends BaseServlet{

    private final TypeService typeService = new TypeServiceImpl();

    /**
     * 功能描述:获得所有书的类型，用于出入库的下拉框生成
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String getAllType(HttpServletRequest request, HttpServletResponse response){
        List<Map> list = typeService.getAllType();
        return JSON.toJSONString(list);
    }

    /**
     * 功能描述:分页、查询条件，查询想相应的类型
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String getAllTypeByPage(HttpServletRequest request,HttpServletResponse response){
        Type type = new Type();
        TransBean.populate(type, request.getParameterMap());
        PageBean page = typeService.getAllTypeByPage(type);
        return JSON.toJSONString(page);
    }

    /**
     * 功能描述:检测添加、修改类型名称是否可用
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String checkName(HttpServletRequest request,HttpServletResponse response){
        Type type = new Type();
        TransBean.populate(type, request.getParameterMap());
        return typeService.checkName(type);
    }

    /**
     * 功能描述:添加、修改类型
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String addType(HttpServletRequest request,HttpServletResponse response){
        Type type = new Type();
        TransBean.populate(type, request.getParameterMap());
        int result = typeService.addType(type);
        return result+"";
    }

    /**
     * 功能描述:删除类型
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String deleteType(HttpServletRequest request,HttpServletResponse response){
        String typeid = request.getParameter("typeid");
        int result = typeService.deleteType(typeid);
        return result+"";
    }

    /**
     * 功能描述:查询各个类型有多少书，用于首页的图表显示
     * @param request Req
     * @param response Res
     * @return java.lang.String
     */
    public String getAllTypeAndBookCount(HttpServletRequest request,HttpServletResponse response){
        List<Map> list = typeService.getAllTypeAndBookCount();
        return JSON.toJSONString(list);
    }

    public String rankingType(HttpServletRequest request,HttpServletResponse response){
        LayDate layDate = new LayDate();
        TransBean.populate(layDate, request.getParameterMap());
        String sql = "select t.typename name,sum(cd.count) value from t_ckdetail cd join t_book b on cd.bookid = b.bookid join t_ck c on c.rukuid = cd.rukuid join t_type t on b.typeid = t.typeid where 1=1 ";
        if (layDate.getYear()!=null){
            sql+=" and year(rdate) = "+layDate.getYear();
        }else{
            sql+=" and year(rdate) = 2020";
        }
        if (layDate.getMonthMin()!=null){
            sql+=" and month(rdate) >="+layDate.getMonthMin();
        }else {
            sql+=" and month(rdate) >= 1";
        }
        if (layDate.getMonthMax()!=null){
            sql+=" and month(rdate) <="+layDate.getMonthMax();
        }else {
            sql+=" and month(rdate) <= 12";
        }
        sql += "  GROUP BY t.typeid ORDER BY value";
        List<Map> list = BaseDb.query(sql);
        System.out.println(list);
        return JSON.toJSONString(list);
    }

}
